'use strict';

const Service = require('egg').Service;

class EssayService extends Service {
  async getEssayTabList() {
    const list = await this.app.mysql.select('tab');
    return { list };
  }

  async getPublishEssayList(tabId, userId) {
    const list = await this.app.mysql
      .query(`SELECT e.id, title, publish_time, e.introduction, name, username, avatar_url,
      (SELECT IFNULL(SUM(is_like),0) FROM essay_like WHERE e.id = essay_id) AS like_count,
      (SELECT IFNULL(SUM(is_collect),0) FROM essay_like WHERE e.id = essay_id) AS collect_count,
      (SELECT IFNULL(COUNT(content),0) FROM comment WHERE e.id = essay_id) AS comment_count,
      IFNULL((SELECT is_like FROM essay_like WHERE e.id = essay_id AND essay_like.user_id = ${userId}),0) AS is_like,
      IFNULL((SELECT is_collect FROM essay_like WHERE e.id = essay_id AND essay_like.user_id = ${userId}),0) AS is_collect
      FROM essay e, tab, user
      WHERE e.tab_id = tab.id AND e.publish_user_id = user.id AND tab.id = ${tabId} AND is_publish = 1`);
    return { list };
  }

  async getUserEssayInfo(userId) {
    const list = await this.app.mysql.query(`SELECT SUM(is_like) AS like_count, SUM(is_collect) AS collect_count
      FROM essay_like
      WHERE user_id = ${userId}`);
    return { list };
  }

  async getSaveEssayList(userId) {
    const list = await this.app.mysql.select('essay', {
      where: { is_publish: 0, publish_user_id: userId },
      columns: ['id', 'title', 'publish_time', 'introduction', 'publish_user_id'],
    });
    return { list };
  }

  async getEssayDetail(essayId) {
    const list = await this.app.mysql.query(
      `SELECT e.id, title, publish_time,introduction, content, publish_user_id, name,
      (SELECT SUM(is_like) FROM essay_like WHERE e.id = essay_id) AS like_count,
      (SELECT SUM(is_collect) FROM essay_like WHERE e.id = essay_id) AS collect_count,
      (SELECT COUNT(content) FROM comment WHERE e.id = essay_id) AS comment_count
      FROM essay e, tab
      WHERE tab_id = tab.id AND e.id = ${essayId}`
    );
    return { list };
  }

  async getEssayInfo(essayId, userId) {
    const res = await this.app.mysql.select('essay_like', {
      where: { essay_id: essayId, user_id: userId },
      columns: ['is_like', 'is_collect'],
    });
    return { res };
  }

  async addEssay(data, is_publish = 0) {
    const res = await this.app.mysql.insert('essay', { ...data, is_publish });
    const id = res.insertId;
    return { id };
  }

  async deleteEssay(essayId, userId) {
    const result = await this.app.mysql.delete('essay', {
      id: essayId,
      publish_user_id: userId,
      is_publish: 0,
    });
    const deleteSuccess = result.affectedRows === 1;
    return { deleteSuccess };
  }

  async likeEssay(essayId, userId) {
    const getItem = await this.app.mysql.get('essay_like', {
      essay_id: essayId,
      user_id: userId,
    });
    let result;
    let insertSuccess = true;
    if (getItem) {
      if (getItem.is_like) return { insertSuccess };
      const data = {
        is_like: 1,
      };
      const options = {
        where: {
          essay_id: essayId,
          user_id: userId,
        },
      };
      result = await this.app.mysql.update('essay_like', data, options);
    } else {
      result = await this.app.mysql.insert('essay_like', {
        essay_id: essayId,
        user_id: userId,
        is_like: 1,
        is_collect: 0,
      });
    }
    insertSuccess = result.affectedRows === 1;
    return { insertSuccess };
  }

  async dislikeEssay(essayId, userId) {
    const data = {
      is_like: 0,
    };
    const options = {
      where: {
        essay_id: essayId,
        user_id: userId,
      },
    };
    const result = await this.app.mysql.update('essay_like', data, options);
    const updateSuccess = result.affectedRows === 1;
    return { updateSuccess };
  }

  async collectEssay(essayId, userId) {
    const getItem = await this.app.mysql.get('essay_like', {
      essay_id: essayId,
      user_id: userId,
    });
    let result;
    let insertSuccess = true;
    if (getItem) {
      if (getItem.is_collect) return { insertSuccess };
      result = await this.app.mysql.update(
        'essay_like',
        {
          is_collect: 1,
        },
        {
          where: {
            essay_id: essayId,
            user_id: userId,
          },
        }
      );
    } else {
      result = await this.app.mysql.insert('essay_like', {
        essay_id: essayId,
        user_id: userId,
        is_like: 0,
        is_collect: 1,
      });
    }
    insertSuccess = result.affectedRows === 1;
    return { insertSuccess };
  }

  async notcollectEssay(essayId, userId) {
    const result = await this.app.mysql.update(
      'essay_like',
      {
        is_collect: 0,
      },
      {
        where: {
          essay_id: essayId,
          user_id: userId,
        },
      }
    );
    const updateSuccess = result.affectedRows === 1;
    return { updateSuccess };
  }
}

module.exports = EssayService;
